// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const query = event.query
  const regNum = /^\d+$/
  const groupsDb = cloud.database().collection('groups')
  // 如果是纯数字
  if (regNum.test(query)) {
    const groupId = parseInt(query)
    const groupData = await groupsDb.where({
      groupId
    }).get()
    const group = groupData.data[0]
    return group
  } else {
    // 如果是普通字符串
    const groupsData = await groupsDb.where({
      groupName: cloud.database().RegExp({
        regexp: `.*${query}`,
        options: 'i'
      })
    }).get()
    const groups = groupsData.data
    return groups
  }
}